// DrawPolyline
/**
 * 绘制线
 */
class DrawPolylineUtils {
  constructor(arg) {
    this.viewer = arg.viewer;
    this.Cesium = arg.Cesium;
    this.callback = arg.callback;
    //这里我们自己再加一个参数,cesiumHandler
    //用于事件的关闭
    this.cesiumHandler = arg.cesiumHandler;
    this._polyline = null; //活动线
    this._polylineLast = null; //最后一条线
    this._positions = [];  //活动点
    this._entities_point = [];  //脏数据
    this._entities_line = [];  //脏数据
    this._polylineData = null; //用于构造线数据
  }

  //返回最后活动线
  get line() {
    return this._polylineLast;
  }

  //返回线数据用于加载线
  getData() {
    return this._polylineData;
  }

  //加载线
  loadPolyline(data) {
    var $this = this;
    var polyline = this.viewer.entities.add({
      polyline: {
        positions: data,
        show: true,
        material: $this.Cesium.Color.RED,
        width: 3,
        clampToGround: true
      }
    });
    return polyline;
  }

  //获取单击的点位列表
  getEntitiesPoint(){
    return function () {
      return this._entities_point;
    };
  }

  //开始创建
  startCreate() {
    var $this = this;
    //这里也不要再new了
    // this.cesiumHandler = new this.Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
    this.cesiumHandler.setInputAction(function (evt) { //单机开始绘制
      //屏幕坐标转地形上坐标
      var cartesian = $this.getCatesian3FromPX(evt.position);
      if ($this._positions.length == 0) {
        $this._positions.push(cartesian.clone());
      }
      $this._positions.push(cartesian);
      $this.createPoint(cartesian);// 绘制点
    }, $this.Cesium.ScreenSpaceEventType.LEFT_DOWN);
    this.cesiumHandler.setInputAction(function (evt) { //移动时绘制线
      if ($this._positions.length < 1) return;
      var cartesian = $this.getCatesian3FromPX(evt.endPosition);
      if (!$this.Cesium.defined($this._polyline)) {
        $this._polyline = $this.createPolyline();
      }
      if ($this._polyline) {
        $this._positions.pop();
        $this._positions.push(cartesian);
      }
    }, $this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    this.cesiumHandler.setInputAction(function (evt) {
      if (!$this._polyline) return;
      var cartesian = $this.getCatesian3FromPX(evt.position);
      $this._positions.pop();
      $this._positions.push(cartesian);
      $this.createPoint(cartesian);// 绘制点
      $this._polylineData = $this._positions.concat();
      $this.viewer.entities.remove($this._polyline); //移除
      $this._polyline = null;
      $this._positions = [];
      var line = $this.loadPolyline($this._polylineData); //加载线
      $this._entities_line.push(line);
      $this._polylineLast = line;
      console.log(89, line);
      if (typeof $this.callback == "function") {
        $this.callback(line);
      }

    }, $this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);

    //取消绘画事件,注意,不再是销毁
    // this.destroy();
    this.cesiumHandler.setInputAction((evt) => {
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.LEFT_DOWN);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
    }, this.Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
  }

  //创建点
  createPoint(cartesian) {
    var $this = this;
    var point = this.viewer.entities.add({
      position: cartesian,
      point: {
        pixelSize: 10,
        color: $this.Cesium.Color.YELLOW,
      }
    });
    $this._entities_point.push(point);
    return point;
  }

  //创建线
  createPolyline() {
    var $this = this;
    var polyline = this.viewer.entities.add({
      polyline: {
        //使用cesium的peoperty
        positions: new $this.Cesium.CallbackProperty(function () {
          return $this._positions
        }, false),
        show: true,
        material: $this.Cesium.Color.RED,
        width: 3,
        clampToGround: true
      }
    });
    $this._entities_line.push(polyline);
    return polyline;
  }

  //销毁
  destroy() {
    if (this.cesiumHandler) {
      // this.cesiumHandler.destroy();
      // this.cesiumHandler = null;
      // this.cesiumHandler.setInputAction(function (evt) {
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.LEFT_DOWN);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
      this.cesiumHandler.removeInputAction(this.Cesium.ScreenSpaceEventType.MOUSE_MOVE);
      // },this.Cesium.ScreenSpaceEventType.RIGHT_CLICK);
    }
  }

  //清空实体对象
  clear() {
    for (var i = 0; i < this._entities_point.length; i++) {
      this.viewer.entities.remove(this._entities_point[i]);
    }
    for (var i = 0; i < this._entities_line.length; i++) {
      this.viewer.entities.remove(this._entities_line[i]);
    }
    this._polyline = null;
    this._positions = [];
    this._entities_point = [];  //脏数据
    this._entities_line = [];  //脏数据
    this._polylineData = null; //用于构造线数据
    this._polylineLast = null;
  }

  getCatesian3FromPX(px) {
    var cartesian;
    var ray = this.viewer.camera.getPickRay(px);
    if (!ray) return null;
    cartesian = this.viewer.scene.globe.pick(ray, this.viewer.scene);
    return cartesian;
  }
}

export default DrawPolylineUtils;
